package com.yeping.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeping.domain.ResponseResult;
import com.yeping.domain.dto.TagListDto;
import com.yeping.domain.entity.Tag;
import com.yeping.domain.entity.User;
import com.yeping.domain.vo.PageVo;
import com.yeping.domain.vo.TagVo;
import com.yeping.domain.vo.UserInfoVo;
import com.yeping.enums.AppHttpCodeEnum;
import com.yeping.exception.SystemException;
import com.yeping.mapper.TagMapper;
import com.yeping.service.TagService;
import com.yeping.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2024-02-29 16:17:09
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
        wrapper.eq(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());

        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,wrapper);
        //封装数据返回
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);

    }

    @Override
    public ResponseResult addTag(Tag tag) {
        if (!StringUtils.hasText(tag.getName())){
            throw new SystemException(AppHttpCodeEnum.TAGNAME_NOT_NULL);
        }
        if (!StringUtils.hasText(tag.getRemark())){
            throw new SystemException(AppHttpCodeEnum.REMARK_NOT_NULL);
        }
        save(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTag(Long tagId) {
        if (tagId == null){
            throw new SystemException(AppHttpCodeEnum.USERID_NOT_NULL);
        }
        removeById(tagId);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult selectTagById(Long tagId) {
        if (tagId == null){
            throw new SystemException(AppHttpCodeEnum.USERID_NOT_NULL);
        }
        Tag tag = getById(tagId);
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagVo);
    }

    @Override
    public ResponseResult updateTagById(Tag tag) {
        if (!StringUtils.hasText(tag.getName())){
            throw new SystemException(AppHttpCodeEnum.TAGNAME_NOT_NULL);
        }
        if (!StringUtils.hasText(tag.getRemark())){
            throw new SystemException(AppHttpCodeEnum.REMARK_NOT_NULL);
        }
        updateById(tag);
        return ResponseResult.okResult();
    }

    @Override
    public List<TagVo> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId,Tag::getName);
        List<Tag> list = list(wrapper);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        return tagVos;
    }

    @Override
    public ResponseResult tagAll(Long pageNum, Long pageSize, String tagName) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(tagName),Tag::getName,tagName);
        Page<Tag> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Tag> list = page.getRecords();

        return ResponseResult.okResult(new PageVo(list,page.getTotal()));
    }


}
